Component({
  /**
   * 组件的属性列表
   */
  properties: {
    selectedData: {
      type: Object,
      value: {}
    },
    filterKey: {
      type: String,
      required: true
    },
    list: {
      type: Array,
      value: []
    },
    selectedKey: {
      type: String,
      value: 'category'
    },
    show: {
      type: Boolean,
      value: false
    },
    dictCode: {
      type: String,
      value: ''
    },
    initData: {
      type: Object,
      value: {}
    },
    filterBatch: {
      type: Array,
      value: []
    },
    categoryBatch: {
      type: Array,
      value: []
    },

    currentType: {
      type: Object,
      value: {}
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    children: [],
    data: {},
    schoolTypeList: [],
    key: '',
    bottomLift: getApp().globalData.bottomLift,
    currentIndex: 0,
    chongCount: '',
    wenCount: '',
    baoCount: '',
  },

  /**
   * 组件的方法列表
   */
  observers: {
    'selectedData': function (selectedData) {
      this.syncData(selectedData);
    },
    "initData": function (e) {
      this.setData({
        data: this.data.initData,
      })
    },
    "currentType": function (e) {
      if (e) {
        this.syncType(e)
      }

    }
  },
  methods: {
    syncType(value) {
      const wenCountArray =value.wen && value.wen.split(':')
      const chongCountArray =value.chong && value.chong.split(':')
      const baoCountArray =value.bao && value.bao.split(':')
      this.setData({
        wenCountArray,
        chongCountArray,
        baoCountArray,
        province: value.province,
        localBatchName: value.localBatchName,
        schoolNum: value.schoolNum,
        voluntaryType: value.voluntaryType,
      })
    },
    syncData(selectedData) {
      let {
        list,
        filterBatch,
        categoryBatch
      } = this.data;
      list.map(cate => {
        const key = cate.key;
        cate.items.map(item => {
          if (cate.type === 'checkbox') {
            const exist = selectedData[key].length > 0 && selectedData[key].indexOf(item.id) >= 0;
            item.checked = exist;
          } else if (cate.type === 'radiobox') {
            item.checked = selectedData[key] === item.id;
          }
          return item;
        });
        return cate;
      })
      filterBatch.map(cate => {
        const key = cate.key;
        cate.items.map(item => {
          if (cate.type === 'checkbox') {
            const exist = selectedData[key].indexOf(item.id) >= 0;
            item.checked = exist;
          } else if (cate.type === 'radiobox') {
            item.checked = selectedData[key] === item.id;
          }
          return item;
        });
        return cate;
      })
      categoryBatch.map((item) => {
        const flag = selectedData['category'] === item.id
        item.checked = flag;
        item.children.map((child) => {
          child.checked = flag && selectedData['categoryId'].indexOf(child.id) > -1;
        })
      })
      this.setData({
        list,
        categoryBatch,
        filterBatch
      })


    },
    close() {
      this.setData({
        show: false
      })
    },
    changeIndex(e) {
      const {
        index
      } = e.currentTarget.dataset
      this.setData({
        currentIndex: index
      })
    },
    checkboxChange(e) {
      const schoolTypeList = e.detail.value
      this.setData({
        schoolTypeList,
      })
    },
    toggleChecked(evt) {
      const {
        checked,
        key,
        id,
        type,
        value
      } = evt.currentTarget.dataset;
      let selectedData = this.data.selectedData
      if (checked) {
        if (Array.isArray(selectedData[key])) {
          selectedData[key].splice(selectedData[key].indexOf(id), 1);
        } else {
          if (key == 'volunteer') return
          selectedData[key] = ''
        }
      } else {
        if (Array.isArray(selectedData[key])) {
          selectedData[key].push(id);
        } else {
          selectedData[key] = id;
        }
      }
      this.setData({
        selectedData,
        currentTab: (type == 'radiobox') ? -1 : this.data.currentTab
      });

    },
    resetBatch(e) {
      let {
        filterBatch,
        selectedData,
        selectedKey,
      } = this.data
      const {
        key
      } = e.currentTarget.dataset
      if (key == 'batch') {
        filterBatch.map((item) => {
          if (item.type === 'checkbox') {
            selectedData[item.key] = []
          } else {
            selectedData[item.key] = 0
          }
        })
      } else if (key == 'category') {
        selectedData['categoryId'] = []
      } else if (selectedKey == 'volunteer') {
        this.setData({
          chongCount: '',
          wenCount: '',
          baoCount: '',
        })
      }
      this.setData({
        selectedData
      })
      this.triggerEvent("onSelectChanged", this.data.selectedData);
    },
    tabConfirmed(evt) {
      const {
        chongCount,
        wenCount,
        baoCount,
        schoolNum,
        selectedData,
        voluntaryType
      } = this.data
      const volunteer = selectedData['volunteer']
      if (volunteer == 'all') {
        if (parseInt(chongCount) + parseInt(wenCount) + parseInt(baoCount) !== schoolNum) {
          wx.showToast({
            title: '填报' + voluntaryType + '数量必须等于' + schoolNum,
            icon: 'none'
          })
          return
        }
      }
      selectedData['wenCount'] = wenCount
      selectedData['chongCount'] = chongCount
      selectedData['baoCount'] = baoCount
      this.setData({
        selectedData
      })
      this.close()
      this.triggerEvent("onSelectChanged", this.data.selectedData);
    },
    inputChange(e) {
      const {
        type
      } = e.currentTarget.dataset
      this.setData({
        [type]: e.detail.value
      })
    },
    checkInput() {
      const {
        chongCount,
        wenCount,
        baoCount,
        schoolNum,
        voluntaryType
      } = this.data
      if (chongCount == '' || wenCount == '' || baoCount == '') return
      if (parseInt(chongCount) + parseInt(wenCount) + parseInt(baoCount) !== schoolNum) {
        wx.showToast({
          title: '填报'+voluntaryType+'数量必须等于' + schoolNum,
          icon: 'none'
        })
      }
    }
  },

})